<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/head::head('','/css/ztree/bootstrapStyle.css','/js/ztree/jquery.ztree.core.min.js,/js/ztree/jquery.ztree.excheck.min.js')">
</head>
<SCRIPT th:inline="javascript">
    /*<![CDATA[*/

    var basePath = /*[[${#httpServletRequest.getScheme() + "://" + #httpServletRequest.getServerName() + ":" + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath()}]]*/ 'basepath';

    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: function (e, treeId, treeNode, clickFlag) {
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.checkNode(treeNode, !treeNode.checked, true);
            }

        }
    };
    $(document).ready(function(){
        var roleId = $("#roleId").val();
        var url = basePath + "/lims/role/gerRolePermissions";
        $.post({
            url:url,
            data:{"roleId":roleId},
            dataType:"json",
            success:function (data) {
                var zNodes = data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.expandAll(true);

            }

        });

    });

    function closeModal(){
        parent.closeModal();
    }

    function ok(){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var checkedNodes = zTree.getCheckedNodes();
        var checkedIds = "";
        for (var i=0;i< checkedNodes.length;i++){
            checkedIds += checkedNodes[i].id+",";
        }
        if (checkedIds.length != 0){
            checkedIds = checkedIds.substring(0,checkedIds.length-1);
        }
        var roleId = $("#roleId").val();
        var url = basePath + "/lims/permission/roleAuthorize";
        $.post({
            url:url,
            data:{"roleId":roleId,"permissionIds":checkedIds},
            dataType:"json",
            success:function(data){
                if(data.code == 0){
                    parent.spop({
                        template: data.msg,
                        position  : 'top-center',
                        style: 'success',
                        autoclose: 2000
                    });
                    closeModal();
                    parent.openMainFrame("/lims/role/roleList");
                }else{
                    parent.spop({
                        template: data.msg,
                        position  : 'top-center',
                        style: 'error',
                        autoclose: 3000
                    });
                }
            }
        });
    }

    /*]]>*/
</SCRIPT>

<body>
    <div class="container">
        <input type="hidden" th:id="roleId" th:name="roleId" th:value="${role.roleId}">
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    <div th:align="right">
        <button type="button" th:onclick="'javascript:ok()'" th:name="submitBtn"  class="btn btn-primary">确定</button>
        <button type="button" th:onclick="'javascript:closeModal()'" class="btn btn-secondary">关闭</button>
    </div>
</body>
</html>